package com.production.CoursePlatform.controller;
import com.production.CoursePlatform.entity.Schedule;
import com.production.CoursePlatform.service.ScheduleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    // 根据 sID 查询 Schedule
    @GetMapping("/findBySID/{sID}")
    public ResponseEntity<Schedule> findBySID(@PathVariable int sID) {
        Schedule schedule = scheduleService.findScheduleBySID(sID);
        if (schedule != null) {
            return ResponseEntity.ok(schedule);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    // 添加 Schedule
    @PostMapping("/add")
    public ResponseEntity<String> addSchedule(@RequestBody Schedule schedule) {
        int result = scheduleService.addSchedule(schedule);
        if (result > 0) {
            return ResponseEntity.status(HttpStatus.CREATED).body("Schedule added successfully");
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to add schedule");
        }
    }

    // 根据 scID 更新 Schedule
    @PutMapping("/update")
    public ResponseEntity<String> updateSchedule(@RequestBody Schedule schedule) {
        int result = scheduleService.updateSchedule(schedule);
        if (result > 0) {
            return ResponseEntity.ok("Schedule updated successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to update schedule");
        }
    }

    // 根据 scID 删除 Schedule
    @DeleteMapping("/delete/{scID}")
    public ResponseEntity<String> deleteSchedule(@PathVariable int scID) {
        int result = scheduleService.deleteSchedule(scID);
        if (result > 0) {
            return ResponseEntity.ok("Schedule deleted successfully");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Failed to delete schedule");
        }
    }

    // 查询所有 Schedule
    @GetMapping("/findAll")
    public ResponseEntity<List<Schedule>> findAllSchedules() {
        List<Schedule> schedules = scheduleService.findAllSchedules();
        return ResponseEntity.ok(schedules);
    }
}


